#pragma once

#include <iostream>
#include <functional>
#include <string>
#include <cstdio>

int mymath(int x, int y, char op)
{
    int ans = 0;
    switch (op)
    {
    case '+':
    {
        ans = x + y;
        break;
    }
    case '-':
    {
        ans = x - y;
        break;
    }
    case '*':
    {
        ans = x * y;
        break;
    }
    case '/':
    {
        if (y == 0)
        {
            std::cout << "除数为0" << std::endl;
            ans = -1;
        }
        else
            ans = x / y;

        break;
    }
    case '%':
    {
        if (y == 0)
        {
            std::cout << "除数为0" << std::endl;
            ans = -1;
        }
        else
            ans = x % y;

        break;
    }
    default:
        break;
    }

    return ans;
}

class task
{
public:
    // typedef int (*func)(int, int, char); // 函数类型
    typedef function<int(int, int, char)> func;

    // 默认构造
    task()
    {
    }

    // 传参构造
    task(int x, int y, char op, func callback)
        : _x(x), _y(y), _op(op), _callback(callback)
    {
    }

    string whatTodo()
    {
        // 格式化任务内容
        char buffer[64] = {0};
        snprintf(buffer, sizeof(buffer), "%d %c %d = ?", _x, _op, _y);

        return buffer;
    }

    /* string showResult()
    {
        // 格式化任务内容
        int result = operator()();
        char buffer[64] = {0};
        snprintf(buffer, sizeof(buffer), "%d %c %d = %d", _x, _op, _y,result);

        return buffer;
    } */

    // operator()重载
    string operator()()
    {
        int result = _callback(_x, _y, _op);
        char buffer[64] = {0};
        snprintf(buffer, sizeof(buffer), "%d %c %d = %d", _x, _op, _y, result);

        return buffer;
    }

private:
    func _callback; // 回调函数
    int _x;         // 左操作数
    int _y;         // 右操作数
    char _op;       // 运算符
};